<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-12-16
 * Time: 10:08
 */

namespace app\water\controller;

use app\common\model\Users;
use app\common\model\Users as UserM;
use think\Db;
use think\Validate;

class Product extends Auth
{
    public $noNeedLogin = [];

    /**
     * 订单列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['limit'] = !empty($post['limit']) ? $post['limit'] : 10;
            $config = Db::name('qt_water_config')->where('store_id', $this->userInfo['store_id'])->find();
            $where = [];
            //不接收的房间
            if (!empty($config['noreceive_room'])) {
                $room_id = explode(',', $config['noreceive_room']);
                $where[] = ['o.room_id','not in',$room_id];
            }

            //不接受的商品分类
            if (!empty($config['noreceive_product'])) {
                $product_cate = explode(',', $config['noreceive_product']);
                //$product_ids = Db::name('qt_goods')->whereIn('goods_category_id', $product_cate)->column('id');
                $where[] = ['g.goods_category_id','not in',$product_cate];
            }

            //展示多少分钟的数据
            if (!empty($config['show_time'])) {
                $time = time() - ($config['show_time'] * 60);
                $where[] = ['oi.create_time','>',$time];
            }

            //是否隐藏确认信息
            if ($config['hidden']  && $config['hidden'] == 1) {
                $is_send = !empty($config['hidden']) && $config['hidden'] == 1 ? 2 : 1;//是否隐藏确认信息
                $where[] = ['oi.is_send', '=',$is_send];
            }
            $orderData = Db::name('qt_product_order_item')
                ->alias('oi')
                ->field('oi.id,oi.goods_name,oi.goods_num,oi.goods_price,oi.create_time,o.room_id,o.uv_id,oi.confirm_time,oi.is_send,o.create_id')
                ->leftJoin('qt_product_order o','oi.order_id=o.id')
                ->leftJoin('qt_goods g','g.id=oi.goods_id')
                ->where($where)
                ->where(['oi.status'=>1])
                ->order('oi.id desc')
                ->paginate($post['limit'])->each(function ($item) {
                    $item['room_name'] = Db::name('qt_room')->where('id', $item['room_id'])->value('name');
                    $item['user_phone'] = Db::name('qt_user_viper')->where('id', $item['uv_id'])->value('phone');
                    if (!empty($item['confirm_time'])) {
                        $item['confirm_time'] = date('Y-m-d H:i:s', $item['confirm_time']);
                        if (empty($item['confirm_time']) || empty($item['create_time'])) {
                            $time = ($item['confirm_time'] - $item['create_time']) / 60;
                        }
                        $item['wait_time'] = empty($time) ? 0 : ceil($time);
                    } else {
                        $time = ceil((time() - $item['create_time']) / 60);
                        $item['wait_time'] = $time;
                    }
                    $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                    $item['create_user'] = Db::name('qt_users')->where('id',$item['create_id'])->value('real_name');
                    unset($item['room_id']);
                    unset($item['uv_id']);
                    return $item;
                });
            $this->result('查询成功', $orderData, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }


    /**
     * 确认订单
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function confirm()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['id']) || empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $update = Db::name('qt_product_order_item')
                ->where(['id' => $getData['id']])
                ->update(['confirm_time' => time(), 'is_send' => 1]);
            if ($update !== false) {
                $this->result('确认成功', '', 200);
            }
            $this->result('确认失败', '', 204);
        }
        $this->result('无效的请求方式', '', 204);
    }

    /**
     * 取消订单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['id']) || empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $productItem = Db::name('qt_product_order_item')->where(['id' => $getData['id']])->find();
            $update = Db::name('qt_product_order')->where('id', $productItem['order_id'])->update(['order_status' => '3', 'cancel_time' => time()]);
            if ($update !== false) {
                $this->result('取消成功', '', 200);
            }
            $this->result('取消失败', '', 204);
        }
        $this->result('无效的请求方式', '', 204);
    }

    /**
     * 是否送达
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveSend()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $order_item = Db::name('qt_product_order_item')
                ->where('id', $post['item_id'])
                ->find();
            if (!empty($order_item)) {
                Db::name('qt_product_order_item')->where('id', $post['item_id'])->update(['is_send' => $post['is_send']]);
            } else {
                $this->result('没有找到数据', '', 204);
            }
            $this->result('操作成功', '', 200);
        }

        $this->result('请求失败', '', 204);
    }


}
